/**
 * 
 */
function checkRefundCost() {
	var money = $("#refund_money").val();
	var otype = $("#refund_money").attr('otype');
	
	if (otype == 1) {
		if (!money) {
			showMessageTip('refund_money', '请填写退款金额', 'error');
			return false;
		}
		var trade_cost = parseFloat($("#refund_money").attr('limit')).toFixed(2);
		var seller_money = parseFloat(trade_cost - parseFloat(money));
		
		if (money && !/^\d+\.?[\d]{0,2}$/.test(money)) {
			showMessageTip('refund_money', '退款金额填写错误', 'error');
			return false;
		}

		if (parseFloat(money) > trade_cost) {
			showMessageTip('refund_money', '退款金额不能超过'+trade_cost+'元', 'error');
			return false;
		}
		
		showMessageTip('refund_money', '', 'success');
		$("#seller_money").text(seller_money.toFixed(2));
		return true;
	} else {
		if (!money) {
			showMessageTip('refund_money', '请填写退款课时数', 'error');
			return false;
		}
		
		var hcost = $("#refund_money").attr('hcost');
		var trade_cost = parseInt($("#refund_money").attr('limit'));
		var seller_money = parseFloat(trade_cost - parseFloat(money))*hcost;
		var buyer_money = (money*hcost).toFixed(2);
		
		if (money && !/^\d+$/.test(money)) {
			showMessageTip('refund_money', '退款课时数填写错误', 'error');
			return false;
		}
		
		money = parseInt(money);
		if (money > trade_cost) {
			showMessageTip('refund_money', '退款课时数不能超过'+trade_cost, 'error');
			return false;
		}
		
		showMessageTip('refund_money', '', 'success');
		$("#seller_money").text(seller_money);
		$("#buyer_money").text(buyer_money);
		return true;
	}
}

$(function(){	
	checkRefundCost();
	
	$("#refund_money").keyup(function(){
		checkRefundCost();
	});

	$("#trade_password").keyup(function(event){
  		if(event.keyCode == 13){
  			$("#saveBtn").click();
  		}
  	});	

	$("#saveBtn").click(function(){
		if (!checkRefundCost()) {
			return false;
		}

		var type = $(this).attr('type');
		
		var oid = $(this).attr('oid');
		
		var otype = $("#refund_money").attr('otype');
		if (otype == 1) {
			var money = $("#refund_money").val();
		} else {
			var hcost = $("#refund_money").attr('hcost');
			var money = $("#refund_money").val()*hcost;
		}
		

		var reason = $("#refund_reason").val();
		if (reason == 0) {
			$.ckAlert({message:"请选择退款原因"});
			return false;
		}

		var desc = $("#refund_desc").val();
		if (!desc) {
			$.ckAlert({message:"请填写退款说明"});
			return false;
		}

		if ($.trim(desc).replace(/[^\x00-\xff]/g, '**').length > 400) {
			$.ckAlert({message:"退款说明做多为400个字符"});
			return false;
		}

		var password = $("#trade_password").val();
		if (!password) {
			$.ckAlert({message:"请填写支付密码"});
			return false;
		}

		if (type == 'add') {
			var url = '/?mod=student&act=refund&do=saveApply';
		} else if (type == 'edit') {
			var url = '/?mod=student&act=refund&do=saveEditApply';
		} else {
			return false;
		}		
		var tipsBoxy = null;
		$.ajax({
			url:url,
			type:'post',
			data:'oid='+oid+'&reason='+reason+'&money='+money+'&desc='+encodeURIComponent(desc)+'&password='+encodeURIComponent(password),
			dataType:'json',
			beforeSend:function(){
				tipsBoxy = $.ckTipsBoxy({type:"loading", message:"", mask:true});
			},
			success:function(ret) {
				if (ret.code == 0) {
					location.replace('/?mod=student&act=refund&do=applySuccess&oid='+oid);
				} else {
					tipsBoxy.cancel();
					$.ckAlert({message:ret.data});
				}
			}
		});
		
		return false;
	});
});